<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台会员账户操作ajax
 * @package  ajax/shop/default
 * @author   allen.qiang
 */

quanli_require('module/func/eai.func.php');

class ajax_account {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询会员账户信息
                    $this->searchCustAccount();
                    break;

                case 2: //查询会员积分
                    $this->searchPoints();
                    break;

                case 3: //查询会员积分账户变更记录
                    $this->searchPointsLog();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    /**
     * 查询会员账户信息（余额、积分）
     */
    private function searchCustAccount() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            $this->toReturn( '会员未登录', 10000);
        }

        $account_array = array(
            'balance' => 0,
            'points' => 0
        );

        //获得会员账户余额
        $balance_array = $G_SHOP->EAI->memberAccount( $G_SHOP->currentPCustID, G_PAYTYPE_CASH, $G_SHOP->DBCA );
        if ($balance_array['code'] == 200) {
            $account_array['balance'] = $balance_array['data']['useableAccount'];
        }

        //获得会员积分
        $points_array = $G_SHOP->EAI->memberAccount( $G_SHOP->currentPCustID, G_PAYTYPE_POINTS, $G_SHOP->DBCA );
        if ($points_array['code'] == 200){
            $account_array['points'] = $points_array['data']['useableAccount'];
        }

        $this->toReturn( 'ok', 200, $account_array );

    }

    /**
     * 查询会员积分
     */
    private function searchPoints() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            $this->toReturn( '会员未登录', 10000);
        }

        $account_array = array(
            'points' => 0
        );

        //获得会员积分
        $points_array = $G_SHOP->EAI->memberAccount( $G_SHOP->currentPCustID, G_PAYTYPE_POINTS, $G_SHOP->DBCA );
        if ($points_array['code'] == 200){
            $account_array['points'] = $points_array['data']['useableAccount'];
        }

        $this->toReturn( 'ok', 200, $account_array );

    }

    /**
     * 查询会员积分账户变更记录
     */
    private function searchPointsLog() {

        global $G_SHOP;

        if (!$G_SHOP->currentPCustID) {
            $this->toReturn( '会员未登录', 10000);
        }

        if (!isset($_REQUEST['page']) || !quanli_is_int($_REQUEST['page'])) {
            $page = 1;
        } else {
            $page = $_REQUEST['page'];
        }
        $rp = 5;

        //实例crm类
        $crm_obj = eai_crm();

        //查询积分变更记录
        $points_account_log_array = $crm_obj->searchCustAccountLog( $G_SHOP->currentPCustID, G_POINTS_ID, $page, $rp, 'seqno', 'DESC');
        if ($points_account_log_array) {

            $result_array['log'] = $points_account_log_array['data'];
            $result_array['paging'] = shop_page($page, $points_account_log_array['count'], $rp, 'javascript:G.points.search(%d);', $G_SHOP->smarty);
            returnJsonData('ok', 200, $result_array);

        } else {
            returnJsonData('积分记录没有找到', 404);
        }

    }

}